package com.studerb.tasks;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;

import com.studerb.tasks.interfaces.ITask;
import com.studerb.tasks.interfaces.ITaskFactory;

public class TaskFactory implements ApplicationContextAware, ITaskFactory {
    private final Logger logger = Logger.getLogger(TaskFactory.class);
    private AbstractApplicationContext applicationContext;

    @Override public ITask getTask(TaskType taskType) {
        String beanName = taskType.beanName();
        logger.debug("Getting task: " + taskType.name() + " using bean: " + beanName);
        return (ITask)applicationContext.getBean(beanName);
    }

    @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (AbstractApplicationContext)applicationContext;
    }
}
